package com.ncmmall.application.business.news;

import com.feijin.commons.lang.Requires;
import com.ncmmall.domain.business.news.News;
import com.ncmmall.domain.business.news.NewsRepository;
import com.ncmmall.domain.business.news.QNews;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.base.Splitter;
import com.google.common.primitives.Longs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公告管理
 *
 * @author likaikai
 * @date 2019-07-11 10:33
 */
@Service
@Transactional
public class NewsApplication {

    @Autowired
    private NewsRepository newsRepository;

    @Autowired
    private QueryChannelService queryChannelService;

    /**
     * 创建公告
     *
     * @param news
     */
    public void create(News news) {
        news.setStatus(1);
        news.setRecommend(1);
        news.setCreateTime(new Timestamp(System.currentTimeMillis()));
        newsRepository.save(news);
    }

    /**
     * 编辑公告
     *
     * @param param
     */
    public void modify(News param) {
        News news = queryChannelService.findOne(QNews.news.id.eq(param.getId()), News.class);
        Requires.notNull(news, "公告不存在");
        news.setTitle(param.getTitle());
        news.setContent(param.getContent());
        newsRepository.save(news);
    }

    /**
     * 推荐到首页
     *
     * @param id
     */
    public void recomend(long id) {
        News news = queryChannelService.findOne(QNews.news.id.eq(id), News.class);
        Requires.notNull(news, "公告不存在");
        news.setRecommend(2);
        newsRepository.save(news);
    }

    /**
     * 取消推荐到首页
     *
     * @param id
     */
    public void notRecomend(long id) {
        News news = queryChannelService.findOne(QNews.news.id.eq(id), News.class);
        Requires.notNull(news, "公告不存在");
        news.setRecommend(1);
        newsRepository.save(news);
    }

    /**
     * 上架
     *
     * @param id
     */
    public void enabled(long id) {
        News news = queryChannelService.findOne(QNews.news.id.eq(id), News.class);
        Requires.notNull(news, "公告不存在");
        news.setStatus(2);
        newsRepository.save(news);
    }


    /**
     * 下架
     *
     * @param id
     */
    public void disable(long id) {
        News news = queryChannelService.findOne(QNews.news.id.eq(id), News.class);
        Requires.notNull(news, "公告不存在");
        news.setStatus(1);
        newsRepository.save(news);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void remove(long id) {
        newsRepository.delete(id);
    }

    /**
     * 批量下架
     *
     * @param data
     */
    public void disables(String data) {
        List<Long> ids = Splitter.on(";").omitEmptyStrings().trimResults().splitToList(data).stream().map(Longs::tryParse).collect(Collectors.toList());
        for (Long id : ids) {
            disable(id);
        }
    }

    /**
     * 批量上架
     *
     * @param data
     */
    public void enables(String data) {
        List<Long> ids = Splitter.on(";").omitEmptyStrings().trimResults().splitToList(data).stream().map(Longs::tryParse).collect(Collectors.toList());
        for (Long id : ids) {
            enabled(id);
        }
    }
}
